//
//  Jiankang_fenxi_jieguo_vc.m
//  ainkjj
//
//  Created by zhiyiLiu on 2020/3/18.
//  Copyright © 2020 zhiyiLiu. All rights reserved.
/*健康分析数据填写，bmi计算，体重身高段位计算气泡显示，并用于请求后台接口显示分析文本；
 */

#import "Jiankang_fenxi_jieguo_vc.h"
#import "SKArchCutter.h"
#import "JBubbleTips.h"
#import "UIView+AZGradient.h"


@interface Jiankang_fenxi_jieguo_vc ()
@property (weak, nonatomic) IBOutlet UILabel *bmi_lb;
@property (weak, nonatomic) IBOutlet UIButton *aixiao_btn;
@property (weak, nonatomic) IBOutlet UIButton *pianai_btn;
@property (weak, nonatomic) IBOutlet UIButton *biaozhun_btn;
@property (weak, nonatomic) IBOutlet UIButton *chaogao_btn;
@property (weak, nonatomic) IBOutlet UIButton *guoqin_btn;
@property (weak, nonatomic) IBOutlet UIButton *tizhong_biaozhun_btn;
@property (weak, nonatomic) IBOutlet UIButton *guozhong_btn;
@property (weak, nonatomic) IBOutlet UIButton *feipang_btn;
@property (weak, nonatomic) IBOutlet UILabel *fenxi1_lb;
@property (weak, nonatomic) IBOutlet UILabel *fenxi2_lb;



@property (nonatomic, copy) NSString *w_code;
@property (nonatomic, copy) NSString *h_code;
@end

@implementation Jiankang_fenxi_jieguo_vc

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"健康信息分析结果";
    
    
    /* 获取 BMI 文案
     api/App/getMesg
     weight    是    string    体重代码
     height    是    string    身高代码
     https://www.showdoc.cc/466016081243223?page_id=3817110580402858
     
     */
    
    [SKArchCutter cuttingView:self.aixiao_btn cuttingDirection:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:self.aixiao_btn.frame.size.height / 2 borderWidth:0 borderColor:[UIColor whiteColor] backgroundColor:[UIColor colorNamed:@"aixiaoColor"]];
    [SKArchCutter cuttingView:self.guoqin_btn cuttingDirection:UIRectCornerTopLeft | UIRectCornerBottomLeft cornerRadii:self.guoqin_btn.frame.size.height / 2 borderWidth:0 borderColor:[UIColor whiteColor] backgroundColor:[UIColor colorNamed:@"aixiaoColor"]];
    
    [SKArchCutter cuttingView:self.chaogao_btn cuttingDirection:UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:self.chaogao_btn.frame.size.height / 2 borderWidth:0 borderColor:[UIColor whiteColor] backgroundColor:[UIColor colorNamed:@"chaogaoColor"]];
    
    [SKArchCutter cuttingView:self.feipang_btn cuttingDirection:UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:self.feipang_btn.frame.size.height / 2 borderWidth:0 borderColor:[UIColor whiteColor] backgroundColor:[UIColor colorNamed:@"chaogaoColor"]];
    

    
    double w = self.w.doubleValue;
    double h = self.h.doubleValue / 100;
    double bmi = w / (h * h);
    self.bmi_lb.text = [NSString stringWithFormat:@"BMI：%.1f" , bmi];
    
    self.h_code = [self heightLevel];
    
    JBubbleTips *tip_v_w = [[JBubbleTips alloc] initWithText:[NSString stringWithFormat:@"%@kg", self.w] direction:JBubbleTipsDirectionDown];

    tip_v_w.bubbleColor = rgb(243, 151, 57);
//    [tip_v az_setGradientBackgroundWithColors:@[rgb(241, 129, 48),rgb(235, 176, 76)] locations:nil startPoint:CGPointMake(0, 0) endPoint:CGPointMake(1, 0)];

    
    if (bmi < 18.4) {
        self.w_code = @"weight-1";
        
        [tip_v_w pointAtView:self.guoqin_btn inView:self.view];
    }else if(bmi <= 23.9){
        self.w_code = @"weight-2";
        [tip_v_w pointAtView:self.tizhong_biaozhun_btn inView:self.view];
    }else if(bmi <= 27.9){
        self.w_code = @"weight-3";
        [tip_v_w pointAtView:self.guozhong_btn inView:self.view];
    }else{
        self.w_code = @"weight-4";
        [tip_v_w pointAtView:self.feipang_btn inView:self.view];
    }
 
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:AuthSignin_model.sharedInstance.data.token forKey:@"Authorization"];
    
    
    [dict setObject:app_code forKey:@"app_code"];
    //    [dict setObject:channel_type forKey:@"channel_type"];
    [dict setObject:self.w_code forKey:@"weight"];
    [dict setObject:self.h_code forKey:@"height"];
    
    [PPHTTPRequest req:@"api/App/getMesg" parameters:dict success:^(id response) {
        NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
        if ([error_code isEqualToString:@"0"]) {
            
            self.fenxi2_lb.text = response[@"data"][@"weight"];
            self.fenxi1_lb.text = response[@"data"][@"height"];
        }
        
        else
            //            if ([error_code isEqualToString:@""])
        {
            
            [MBProgressHUD showTipMessageInView:response[@"error_msg"]];
            
        }
    } failure:^(NSError *error) {
        [MBProgressHUD showTipMessageInView:[error localizedDescription]];
        
    }];
    
    
}

- (NSString *)heightLevel{
    // http://www.tt120.org/aricle.asp?id=152
    CGFloat baseHeight = [self.nan_nv isEqualToString:@"男"] ? 50.4 : 49.7;
    CGFloat n = 25 / 12;
    CGFloat i = 5 / 12;
    CGFloat age = [self dates_days];
    CGFloat standardHeight = (age > 12 ? i * (age - 12) + 25 : n * age) + baseHeight;
    CGFloat percent = self.h.floatValue / standardHeight;
    
   

    JBubbleTips *tip_v = [[JBubbleTips alloc] initWithText:[NSString stringWithFormat:@"%@cm", self.h] direction:JBubbleTipsDirectionDown];
    tip_v.bubbleColor = rgb(243, 151, 57);
    
    if (percent < 0.93) {
        
        [tip_v pointAtView:self.aixiao_btn inView:self.view];
        return @"height-1";
        
    }else if (percent < 0.965) {
        
        [tip_v pointAtView:self.pianai_btn inView:self.view];
        return @"height-2";
    }else if (percent < 1.06) {
        
        [tip_v pointAtView:self.biaozhun_btn inView:self.view];
        return @"height-3";
    }else{
        
        [tip_v pointAtView:self.chaogao_btn inView:self.view];
        return @"height-4";
    }
    
}

- (NSInteger)dates_days{
    
    //创建两个日期
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *startDate = [dateFormatter dateFromString:self.birth_date];
    NSDate *endDate = [NSDate date];
    
    //利用NSCalendar比较日期的差异
    NSCalendar *calendar = [NSCalendar currentCalendar];
    /**
     * 要比较的时间单位,常用如下,可以同时传：
     *    NSCalendarUnitDay : 天
     *    NSCalendarUnitYear : 年
     *    NSCalendarUnitMonth : 月
     *    NSCalendarUnitHour : 时
     *    NSCalendarUnitMinute : 分
     *    NSCalendarUnitSecond : 秒
     */
    NSCalendarUnit unit = NSCalendarUnitMonth;//只比较天数差异
    //比较的结果是NSDateComponents类对象
    NSDateComponents *delta = [calendar components:unit fromDate:startDate toDate:endDate options:0];
    //打印
    NSLog(@"%@",delta);
    //获取其中的"天"
    NSLog(@"%ld",delta.month);
    return delta.month;
}
@end
